package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    ApartmentFacilityMapper apartmentFacilityMapper;

    @Autowired
    ApartmentLabelMapper apartmentLabelMapper;

    @Autowired
    ApartmentFeeValueMapper apartmentFeeValueMapper;

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    ApartmentFacilityService apartmentFacilityService;

    @Autowired
    ApartmentLabelService apartmentLabelService;

    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    CityInfoMapper cityInfoMapper;

    @Autowired
    DistrictInfoMapper districtInfoMapper;

    @Autowired
    ProvinceInfoMapper provinceInfoMapper;

    @Autowired
    LabelInfoMapper labelInfoMapper;

    @Autowired
    FacilityInfoMapper facilityInfoMapper;

    @Autowired
    FeeValueMapper feeValueMapper;

    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> apartmentItemVoIPage, ApartmentQueryVo queryVo) {
        IPage<ApartmentItemVo> itemVoIPage = baseMapper.selectPageItem(apartmentItemVoIPage, queryVo);
        return itemVoIPage;
    }

    @Override
    public void saveOrUpdateApartmentInfo(ApartmentSubmitVo apartmentSubmitVo) {

        Long id = apartmentSubmitVo.getId();
        if (id != null) {
            // 修改 ，删除旧数据

            QueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new QueryWrapper<>();
            apartmentFacilityQueryWrapper.eq("apartment_id", id);
            apartmentFacilityMapper.delete(apartmentFacilityQueryWrapper);

            QueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new QueryWrapper<>();
            apartmentLabelQueryWrapper.eq("apartment_id", id);
            apartmentLabelMapper.delete(apartmentLabelQueryWrapper);

            QueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new QueryWrapper<>();
            apartmentFeeValueQueryWrapper.eq("apartment_id", id);
            apartmentFeeValueMapper.delete(apartmentFeeValueQueryWrapper);

            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
            graphInfoQueryWrapper.eq("item_type", 1).eq("item_id", id);
            graphInfoMapper.delete(graphInfoQueryWrapper);

            apartmentSubmitVo.setUpdateTime(null); //更新的时候需要自动填充时间
        }

        // 保存公寓信息
        Long districtId = apartmentSubmitVo.getDistrictId();
        Long cityId = apartmentSubmitVo.getCityId();
        Long provinceId = apartmentSubmitVo.getProvinceId();

        String districtName = districtInfoMapper.selectById(districtId).getName();
        String cityName = cityInfoMapper.selectById(cityId).getName();
        String provinceName = provinceInfoMapper.selectById(provinceId).getName();

        apartmentSubmitVo.setDistrictName(districtName);
        apartmentSubmitVo.setCityName(cityName);
        apartmentSubmitVo.setProvinceName(provinceName);
        // 在这里可以做一些关键参数的校验
        this.saveOrUpdate(apartmentSubmitVo);
        Long apartmentId = apartmentSubmitVo.getId();

        // 保存配套中间表
        List<Long> facilityIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        for (Long facilityId : facilityIds) {
            ApartmentFacility apartmentFacility = ApartmentFacility
                    .builder()
                    .apartmentId(apartmentId)
                    .facilityId(facilityId)
                    .build();
            apartmentFacilities.add(apartmentFacility);
        }
        apartmentFacilityService.saveBatch(apartmentFacilities);

        // 保存标签中间表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> apartmentLabels = new ArrayList<>();
        for (Long labelId : labelIds) {
            ApartmentLabel apartmentLabel = ApartmentLabel
                    .builder()
                    .apartmentId(apartmentId)
                    .labelId(labelId)
                    .build();
            apartmentLabels.add(apartmentLabel);
        }
        apartmentLabelService.saveBatch(apartmentLabels);

        // 保存杂费中间表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        for (Long feeValueId : feeValueIds) {
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue
                    .builder()
                    .apartmentId(apartmentId)
                    .feeValueId(feeValueId)
                    .build();
            apartmentFeeValues.add(apartmentFeeValue);
        }
        apartmentFeeValueService.saveBatch(apartmentFeeValues);

        // 保存图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfos = new ArrayList<>();
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo = new GraphInfo();
            BeanUtils.copyProperties(graphVo, graphInfo);
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setItemId(apartmentId);
            graphInfos.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfos);


    }

    @Override
    public ApartmentDetailVo getDetailById(Integer id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = baseMapper.selectById(id);
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

        List<LabelInfo> labelInfoList = new ArrayList<>();
        labelInfoList = labelInfoMapper.selectListByApartmentId(id);
        apartmentDetailVo.setLabelInfoList(labelInfoList); //标签信息

        List<FacilityInfo> facilityInfoList = new ArrayList<>();
        facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList); //配套信息

        List<FeeValueVo> feeValueVoList = new ArrayList<>();
        feeValueVoList=feeValueMapper.selectListByApartmentId(id);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList); // 费用信息

        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type", 1).eq("item_id", id);

        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoQueryWrapper);
        List<GraphVo> graphVoList = graphInfos.stream().map(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            return graphVo;
        }).collect(Collectors.toList());
        apartmentDetailVo.setGraphVoList(graphVoList); // 图片信息

        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        // 删除图片
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type", 1).eq("item_id", id);
        graphInfoMapper.delete(graphInfoQueryWrapper);
        // 删除配套中间表
        QueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new QueryWrapper<>();
        apartmentFacilityQueryWrapper.eq("apartment_id", id);
        apartmentFacilityMapper.delete(apartmentFacilityQueryWrapper);
        // 删除标签中间表
        QueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new QueryWrapper<>();
        apartmentLabelQueryWrapper.eq("apartment_id", id);
        apartmentLabelMapper.delete(apartmentLabelQueryWrapper);
        // 删除杂费中间表
        QueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new QueryWrapper<>();
        apartmentFeeValueQueryWrapper.eq("apartment_id", id);
        apartmentFeeValueMapper.delete(apartmentFeeValueQueryWrapper);
        // 删除公寓



        // 删除图片
//        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
//        graphInfoQueryWrapper.eq("item_type",1);
//        graphInfoQueryWrapper.eq("item_id",id);
//        graphInfoMapper.delete(graphInfoQueryWrapper);
//        // 删除配套中间表
//        LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
//        facilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
//        apartmentFacilityService.remove(facilityQueryWrapper);
//        // 删除标签中间表
//        LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
//        labelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
//        apartmentLabelService.remove(labelQueryWrapper);
//
//        // 删除杂费中间表
//        LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
//        feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
//        apartmentFeeValueService.remove(feeQueryWrapper);

        // 删除公寓
        this.removeById(id);
    }
}




